package com.wgj.android.adapter;

import android.content.Context;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiaai.xui.utils.XDateUtils;
import com.jiaai.xui.utils.imageload.ImageLoader;
import com.jiaai.xui.utils.imageload.XImage;
import com.wgj.android.R;
import com.wgj.android.events.Adver;
import com.wgj.android.events.ViedoEvent;
import com.wgj.android.tools.MyJZVideoPlayerStandard;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.crypto.spec.IvParameterSpec;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * Created by Administrator on 2018/1/31.
 */

public class AdverAdapter extends RecyclerView.Adapter<AdverAdapter.PictureHolder> implements View.OnClickListener{
    private  Context mContext;
    private List<Adver> advers= new ArrayList<>();

    public  AdverAdapter(Context context){
        mContext=context;

    }
    @Override
    public PictureHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(mContext).inflate(R.layout.adver_item,parent,false);
            view.setOnClickListener(this);
            return  new PictureHolder(view);
    }

    @Override
    public void onBindViewHolder(PictureHolder holder, int position) {
        if(advers.get(position).getIs_get()==1){
            holder.tag.setText("已领取");
            holder.tag.setBackgroundResource(R.drawable.adver_no_tag);
        }else {
            holder.tag.setText("红包");
            holder.tag.setBackgroundResource(R.drawable.adver_select_tag);
        }
        if(advers.get(position).getType()==1){
            holder.iv_play.setVisibility(View.GONE);
        }
        holder.tv_num.setText(advers.get(position).getReleaseNum()+"");
        holder.tv_title.setText(advers.get(position).getTitle());
        holder.tv_time.setText(XDateUtils.format(new Date(advers.get(position).getCreateTime()),"yyyy-MM-dd"));
        XImage.getInstance().load(holder.iv_icon,advers.get(position).getThumbnail());
        holder.itemView.setTag(position);

    }

    @Override
    public int getItemCount() {
        return advers.size();
    }


    private OnItemClickListener mOnItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }


    class  PictureHolder extends  RecyclerView.ViewHolder{
        private TextView tag;
        private ImageView iv_icon;
        private TextView tv_title;
        private TextView tv_time;
        private  TextView tv_num;
        private ImageView iv_play;


        public PictureHolder(View itemView) {
            super(itemView);
            iv_icon=itemView.findViewById(R.id.iv_icon);
            tag=itemView.findViewById(R.id.tag);
            tv_title=itemView.findViewById(R.id.tv_title);
            tv_time=itemView.findViewById(R.id.tv_time);
            tv_num=itemView.findViewById(R.id.tv_num);
            iv_play=itemView.findViewById(R.id.iv_play);
        }

    }

    public  void update(List<Adver> advers){
        this.advers.clear();
        this.advers.addAll(advers);
        notifyDataSetChanged();
    }
}
